package com.its.us.web.controller.sys;

import com.its.us.mq.rocketmq.TestSendMsg;
import com.its.us.web.entity.sys.User;
import com.its.us.web.service.sys.UserService;
import com.its.us.web.vo.sys.ResultBean;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Author: master
 * @Date: 2020/11/5 0:04
 * @Version: 1.0
 */
@Api(tags = "系统用户")
@RestController
@RequestMapping("user")
@Slf4j
public class UserLoginController {

    @Resource
    private UserService userService;

    @Resource
    private TestSendMsg testSendMsg;


    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     */
    @ApiOperation(value = "用户登录", notes = "用户登录返回token", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "登录名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "{code:200,sub:'admin',msg:'登录成功.',tokenAz:'xxxxxxx','perm':'xxxx'}"),
            @ApiResponse(code = 403, message = "{code:403,msg:'密码错误.'}"),
            @ApiResponse(code = 404, message = "{code:404,msg:'用户不存在.'}")
    })
    @PostMapping("login")
    public Map login(String username, String password) {
        Map map = null;
        try {
            map = userService.login(username, password);

            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            // 异步发送
            //testSendMsg.sendMsg(user);
        } catch (Exception e) {
            log.error("登录错误：", e);
        }
        return map;
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation(value = "用户退出", notes = "用户登录返回token", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "登录名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "{code:0,msg:'success'}"),
            @ApiResponse(code = 500, message = "{code:500,msg:'throwable cause'}")
    })
    @GetMapping("logout")
    public ResultBean logout() {
        try {
            /*HttpSession session = request.getSession(false);
            if (session != null) {
                session.invalidate();
            }*/
            SecurityContextHolder.getContext().setAuthentication(null);
        } catch (Exception e) {
            log.error("退出错误：", e);
            return new ResultBean(e.getCause());
        }
        return new ResultBean("退出成功");
    }


}
